<%-- <%@page import="my.model.com.ArticleDTO"%> --%>
<%@page import="my.db.DAO.ArticleDAO"%>
<%@page import="my.db.DTO.ArticleTypeDTO"%>
<%@page import="my.db.DTO.ArticleDTO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="javax.sql.DataSource"%>
<%@ include file="sites/ad_header.jsp" %>
<%
	if(session.getAttribute("user") ==null){
		response.sendRedirect("LoadIndex.jsp");
	}
%>
<%
	ArticleDAO artDAO = new ArticleDAO();
	String keyword = request.getParameter("keyword") == null ? "" : request.getParameter("keyword");
		
	int pages = request.getParameter("pages") == null ? 0 : Integer.parseInt(request.getParameter("pages"));
	int rows = Integer.parseInt(request.getParameter("rows"));
	int first = 1;
	int userid=(Integer)session.getAttribute("userid");
	int last = (int) Math.ceil((double) artDAO.countRowsArticleAuthor(keyword, userid) / rows);
	
%>
	<div class="r-body">
				<div class="page-title">
					<p>Article Information</p>
				</div>
				<div class="search-panel">
					<input type="text" id="text-search" name="search_article" class="txt-search" placeholder="Type book title to search.."/>
					<select name="search_booktype" class="option-search">
					<%
						ArrayList<ArticleTypeDTO> lst_artype=(ArrayList<ArticleTypeDTO>)request.getAttribute("list_articletype");
						for(int i=0;i<lst_artype.size();i++){
					%>
						<option value=<%=lst_artype.get(i).getTypeID() %>>
							<%=lst_artype.get(i).getTypeName() %>
						</option>
					<%}%>					
					</select>
				</div><!-- end search panel -->
				</table>			
		<%
			String error=request.getParameter("error");
			if(error !=null){
				out.println(error);
			}else{
				error="";
				out.println(error);
			}
		%>		
				<div class="grid-data">
				<form action="./delete_article.article" method="post" name="frm_delete_article">
				
					<table border="0" class="tb-data" cellpadding="8">
						<thead>
							<tr>
								<th width="28"><input type="checkbox" class="checkall" name="checkall"/></th>
								<th width="10%">Book ID</th>
								<th width="40%">Book Title</th>
								<th>Public Date</th>
								<th width="130"><a href="./New_Article.article"><img src="Image/New.png" title=" Add new author"/></a> &nbsp;&nbsp;
									
									<a href="#" onclick="deleteArticle()"  class="delete" style="padding:8px;background:#fff;display:inline-block;position:relative;top:-8px;margin-bottom:-10px;">
										<img src="Image/r_delete.png" title="Delete Author" />
									</a>
									
								</th>
							</tr>
						</thead>
						<tbody>
						<%
							ArrayList<ArticleDTO> lst_ar=(ArrayList<ArticleDTO>)request.getAttribute("list_article");
							if(lst_ar!=null){
							for(int i=0;i<lst_ar.size();i++){
							ArticleDTO dto=lst_ar.get(i);
						%>
						<tr>
							<td class="td-center">
								<input  type="checkbox" name="check_id" value="<%=dto.getArticleNo()%>" class="check_id"/>
							<td class="td-center">
								<%=dto.getArticleNo() %>
							</td>
							<td>
								<%=dto.getArticleTitle() %>
							</td>						
							<td class="td-center">
								<%=dto.getArticleDate() %>
							</td>
							<td class="td-center">
									<a href="./edit_article.article?id=<%=dto.getArticleNo()%>"><img src="Image/edit.png" title="Edit Author"/></a>&nbsp;&nbsp;									
									<a href="#" onclick="article_detail(<%=dto.getArticleNo()%>)"><img src="Image/detail.png" title="Author Detail"/></a>
							</td>
						</tr>
						<%}}%>
						<%-- <%}	%> --%>												
						</tbody>						
					</table>
					
					</form>
				</div><!-- end datagrind -->
				
				<!-- Pagination List -->
				<div class="pagination-panel">
           			 <div class="pagination">
           			 		<ul>
	           			 		<li>
	           			 			<a class="last_p" href="./list_article.article?rows=3&pages=1&keyword=<%=keyword%>"> << </a>
	           			 		</li>
								<li>
									<a class="next_p" href="./list_article.article?rows=3&pages=<%=pages - 1 < 2 ? first : pages - 1%>&keyword=<%=keyword%>"> < </a> 
								</a></li>
								
								 <%
						 			String style="style='color:red'";
						            for(int i=1; i<=last; i++){
						            	if (i==pages){
					            %>
					            <li>
					            	<a href="./list_article.article?rows=<%=rows%>&pages=<%=i%>&keyword=<%=keyword%>"><%=i%></a>
					            </li>
					            	
				           		<%
				           			} else {
				           		%>
				           		<li>
				           			<a href="./list_article.article?rows=<%=rows%>&pages=<%=i%>&keyword=<%=keyword%>"><%=i%></a>
				           		</li>		
				           		<%
				           			}
					            }%>
								
								<li>
									 <a class="prev" href="./list_article.article?rows=3&pages=<%= pages + 1 > last ? last : pages + 1%>&keyword=<%=keyword%>"> > </a>
								</li>
								<li>
									<a class="first" href="./list_article.article?rows=3&pages=<%=last%>&keyword=<%=keyword%>"> >> </a>
								</li>
           			 		</ul>    
           			 		
						<div class="go-page">
							<input type="text" class="go-to-page number"/>
							<input type="button" value=" GO "/>
						</div>
           			</div>
				</div><!-- end pagination -->
				<!-- Edit BookContent -->
					
				<!-- End Edit BookContent -->
				
		<script language="javascript" type="text/javascript">
				function deleteArticle() {
			    		var r = confirm("Do you want to delete?");
			    		if (r == true) {
			       			window.location.href="javascript:deleteUsers()";
			    		}  
				} 
				function deleteUsers(){
					frm_delete_article.submit();
				}
				function article_detail(id){
					window.location.href="./list_article.article?articledetailid="+id+"";
				} 
	</script>
	<script type="text/javascript">
		$(function(){
			$(".checkall").change(function(){
				var ch=$(this).prop("checked");
				$(".check_id").prop("checked",ch);
			});
		});
		$(document).ready(function(){
			 $('#text-search').keypress(function(e){
			      if(e.keyCode==13){
			    	  var key = document.getElementById("text-search").value;
						window.location.href="./list_article.article?rows=3&pages=1&keyword="+ key;
			      }
			      
			   });
		});
	</script>
			</div><!-- end body -->
</div><!-- end r-main -->
</body>
</html>